package com.example.tuike.controller;

import com.example.tuike.entity.User;
import com.example.tuike.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/*** @Author supersaiya
 * @Description

 * @Date 2025/1/28 16:29

 * @Version 1.0*/
@Tag(name = "用户管理", description = "用户注册、登录、信息维护等操作")
@RestController
@RequestMapping("/api/users")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @Operation(
            summary = "创建用户",
            description = "注册新用户接口",
            responses = {
                    @ApiResponse(responseCode = "200", description = "用户创建成功"),
                    @ApiResponse(responseCode = "400", description = "参数校验失败")
            }
    )
    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        return ResponseEntity.ok(userService.createUser(user));
    }
    @Operation(
            summary = "获取用户详情",
            parameters = @Parameter(name = "userId", description = "用户ID", example = "1")
    )
    @GetMapping("/{userId}")
    public ResponseEntity<User> getUser(@PathVariable Integer userId) {
        return ResponseEntity.ok(userService.getUserById(userId));
    }

    @PutMapping("/{userId}")
    public ResponseEntity<User> updateUser(
            @PathVariable Integer userId,
            @RequestBody  User user) {
        user.setUserId(userId);
        userService.updateUser(user);
        return ResponseEntity.ok(user);
    }

    @DeleteMapping("/{userId}")
    public ResponseEntity<Void> deleteUser(@PathVariable Integer userId) {
        userService.deleteUser(userId);
        return ResponseEntity.noContent().build();
    }
}
